package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.LoginModel;
import com.jiayang.hhotel.presenter.Plogin;
import com.jiayang.hhotel.util.IOUtil;
import com.jiayang.hhotel.util.StringUtil;

import cn.droidlover.xdroidmvp.custom.TitleListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import cn.droidlover.xdroidmvp.net.NetError;

/**
 * Created by Administrator on 2017/5/16 0016.
 * 登录界面
 */

public class LoginActivity extends XActivity<Plogin> implements View.OnClickListener, TitleListener {

    private EditText phone_num_et;
    private EditText psw_et;
    private TextView login_btn,forgetPsd;
    @Override
    public void initData(Bundle savedInstanceState) {
        initView();
    }
    private void initView(){
        initTitle(this,true,getString(R.string.login),R.color.orange_color,getString(R.string.register),this);
        phone_num_et=(EditText)findViewById(R.id.phone_num_et);
        psw_et=(EditText)findViewById(R.id.psw_et);
        login_btn=(TextView) findViewById(R.id.login_btn);
        forgetPsd= (TextView) findViewById(R.id.tv_forget_psd);
        login_btn.setOnClickListener(this);
        forgetPsd.setOnClickListener(this);

        long accountNum = IOUtil.getLong(Constant.USER_NAME,0L);
        if(accountNum!=0){
            phone_num_et.setText(String.valueOf(accountNum));
        }
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public Plogin newP() {
        return new Plogin();
    }

    Intent intent;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_btn:
                String mobile =phone_num_et.getText().toString();
                String psw=psw_et.getText().toString();
                if(!StringUtil.isEmpty(mobile)&&!StringUtil.isEmpty(psw)){
                    getP().login(Long.valueOf(mobile), Constant.LAUGUAGE_FLAG_CHINESE, psw, Constant.getIMEI());
                }
                break;
            case R.id.tv_forget_psd://忘记密码
                intent=new Intent(this,ForgetPswActivity.class);
                startActivity(intent);
                break;
        }
    }

    public void loginsuccess(LoginModel model){
        if(model.getState()==9){
//            for(int i=0;i<model.getLogin().getPRightIdDtolist().size();i++){
//                App.issueList.add(model.getLogin().getPRightIdDtolist().get(i).getCode());
//                if(App.most_issueList.contains(model.getLogin().getPRightIdDtolist().get(i).getCode())){
//                    App.have_issueList.add(model.getLogin().getPRightIdDtolist().get(i).getCode());
//                }
//            }
            IOUtil.saveObject(Constant.JURISDICTION_SHARED_KEY,model.getLogin().getPRightIdDtolist());
            Intent intent =new Intent(this,FrontActivity.class);
            startActivity(intent);
            finish();
        }else{
            getvDelegate().toastShort("请求错误码："+model.getState());
        }
    }
    public void showError(NetError error){
        getvDelegate().toastShort(error.getMessage());
    }

    @Override
    public void rightClickListener() {
        intent=new Intent(this,RegistActivity.class);
        startActivity(intent);
    }
}
